#!/bin/bash
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright (c) 2019, Joyent, Inc.
#

#
# Generates the SmartOS vmware image
#

set -o nounset
shopt -s xpg_echo
set -o pipefail
export PATH=/usr/bin:/opt/local/bin

bi_wsroot=$(cd $(dirname $0)/../; pwd)
bi_arg0=$(basename $0)
bi_type=${bi_arg0/build_/}

. "${bi_wsroot}/tools/lib/build_common.sh"

bi_template_name='SmartOS.vmwarevm'
bi_usb_image=

function bi_ensure_usb_image
{
	if [[ -z "${bi_platform_name}" ]]; then
		fail "\$bi_platform_name not set"
	fi
	bi_usb_image="output-usb/${bi_platform_name}.usb.gz"

	bi_emit_start 'Looking for USB image'
	if [[ -f "${bi_usb_image}" ]]; then
		bi_emit_done
		bi_emit_info 'Using prebuilt USB image' \
		    "$(basename ${bi_usb_image})"
		return
	else
		bi_emit_done
		bi_emit_info 'USB image not found; must build' \
		    "${bi_usb_image}"
	fi

	bi_emit_start 'Building USB image'
	bi_emit_newline
	if ! ${bi_wsroot}/tools/build_boot_image -r ${bi_wsroot}; then
		fail 'Failed to build USB image'
	fi
	bi_emit_done

	bi_emit_start 'Looking for USB image (again)'
	if [[ ! -f "${bi_usb_image}" ]]; then
		fail 'USB image not found after build.  Aborting.'
	fi
	bi_emit_done
	bi_emit_info 'Using USB image' "$(basename ${bi_usb_image})"
}

function bi_ensure_fresh_template
{
	bi_emit_start 'Create output directory'
	if ! mkdir -p "${bi_output_dir}"; then
		fail "could not mkdir ${bi_output_dir}"
	fi
	bi_emit_done

	if [[ -d "${bi_output_dir}/${bi_template_name}" ]]; then
		bi_emit_start 'Removing existing template directory'
		if ! rm -rf "${bi_output_dir}/${bi_template_name}"; then
			fail 'could not remove directory'
		fi
		bi_emit_done
	fi

	bi_emit_start 'Copying VMware template into output directory'
	if ! cp -r "${bi_wsroot}/tools/${bi_template_name}" \
	    "${bi_output_dir}/${bi_template_name}"; then
		fail 'could not copy VMware template'
	fi
	bi_emit_done
}

function bi_unpack_usb_image
{
	bi_emit_start 'Unpacking USB image into template'
	if ! gunzip --decompress --stdout "${bi_usb_image}" \
	    >"${bi_output_dir}/${bi_template_name}/smartos.img"; then
		fail 'could not unpack USB image'
	fi
	bi_emit_done
}

function bi_generate_vmware_tar
{
	local output_name
	local output_file

	output_name=$(echo "${bi_platform_name}" | sed 's/^platform/smartos/')
	output_file="${output_name}.vmwarevm.tar.gz"

	bi_emit_start 'Generating VMware tarball'
	if ! (cd "${bi_output_dir}" && gtar -I pigz -cf "${output_file}" \
	    "${bi_template_name}"); then
		fail 'gtar failed'
	fi
	bi_emit_done

	bi_emit_info 'VMware Output' "$(pwd)/${bi_output_dir}/${output_file}"
}

while getopts "r:" c $@; do
	case "$c" in
	r)	bi_wsroot=$(readlink -f $OPTARG) ;;
	:)	usage ;;
	*)	usage ;;
	esac
done

#
# Get us to the root of the workspace
#
if ! cd ${bi_wsroot}; then
	fail "could not chdir to workspace root \"${bi_wsroot}\""
fi

bi_get_build
bi_ensure_usb_image
bi_ensure_fresh_template
bi_unpack_usb_image
bi_generate_vmware_tar
